﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace nToDo
{
    public static class Extensions
    {
        public static void GenerateXml(this Collection<Task> collection, string fileName)
        {
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                XmlRootAttribute xmlRoot = new XmlRootAttribute("nTodoTasks");
                XmlSerializer ser = new XmlSerializer(typeof(Collection<Task>), xmlRoot);
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add(string.Empty, string.Empty);
                ser.Serialize(sw, collection, ns);
                ser = null;
            }
        }

        public static Collection<Task> GenerateTasks(this XmlDocument collection, string fileName)
        {
            Collection<Task> result = default(Collection<Task>);
            using (StreamReader sr = new StreamReader(fileName))
            {
                XmlRootAttribute xmlRoot = new XmlRootAttribute("nTodoTasks");
                XmlSerializer ser = new XmlSerializer(typeof(Collection<Task>), xmlRoot);
                result = ser.Deserialize(sr) as Collection<Task>;
                ser = null;
            }
            return result;
        }
    }
}
